<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>使用组件的细节点</title>
    <script src="./js/vue.js"></script>
</head>
<body>
    <div id="root">
        <table>
            <tbody>
                <tr is="row"></tr>
                <tr is="row"></tr>
                <tr is="row"></tr>
            </tbody>
        </table>
        <ul>
            <!-- <ul-li></ul-li> -->
            <li is="ulLi"></li>
        </ul>

        <!-- ref使用 -->
        <div ref="hello" @click="handleClick">hello vue</div>

        <!-- ref定义在组件中 一个小的计数器-->
        <div>
            <counter ref="compOne" @change="handleChange"></counter>
            <counter ref="compTwo" @change="handleChange"></counter>
            {{total}}
        </div>
    </div>
    <script>
        Vue.component('row',{
            data: function(){
                return {
                    content: "this is component tr"
                }
            },
            template: "<tr><td>{{content}}</td></tr>"
        });
        Vue.component('ulLi',{
            template: "<li>this is component tr</li>"
        });

        // 在组件中使用ref
        Vue.component('counter', {
            data: function (){
                return {
                    number: 0,
                }
            },
            template: '<div @click="handleClick">{{number}}</div>',
            methods: {
                handleClick: function(){
                    this.number ++;
                    this.$emit('change');
                }
            }
        })
        var vm = new Vue({
            el: "#root",
            data: {
                total: 0,
            },
            methods: {
                // handleClick: function(){
                //     // 获取ref名字为hello的dom节点
                //     alert(this.$refs.hello.innerHTML);
                // }

                handleChange: function() {
                    // 在子组件上使用ref引用得到的是一个组件的引用
                    this.total = this.$refs.compOne.number + this.$refs.compTwo.number;
                }
            }
        });
    </script>
</body>
</html>